<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
/**
 *
 * @author RAMIANDRISOA Domoina
 * @tel 261 34 13 306 32
 * @email domonaalivelo@gmail.com
 */
class Patient extends CI_Controller {
    //création attribut id_patient = pour accèder à l'identifiant du patient au nniveau de la classe

	function __construct()
	    {
		//parent::__construct();
		//$this->load->helper(array('form', 'url'));
		//$this->load->library('session');
		parent::__construct();
		$this->load->helper('url');
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->load->library('session');
                $this->load->library('input');
                $this->load->model('mod_patient');
	    }

	function index()
	{
		$this->load->view('patient/vue_updatePatient');
	}

	function ajoutP1 (){
                $this->form_validation->set_rules('email','"Adresse mail"','trim|required|xss_clean|valid_email|is_unique[COMPTE.EMAIL]');
                $this->form_validation->set_rules('civilite','"Civilite"','required');
                $this->form_validation->set_rules('nom','"Nom"','trim|required|xss_clean');
                $this->form_validation->set_rules('prenom','"Prenom"','trim|required|xss_clean');
                $this->form_validation->set_rules('password_patient','"Mot de passe"','trim|required|xss_clean');
                $this->form_validation->set_rules('lu','"Cocher condition d\'utilisation"','required');
		if ($this->form_validation->run()==FALSE){
                    echo '<script>document.location="../";</script>';
                } else {
			$email = $this->input->post('email');
				$data = array(
					'CIVILITE_PA' => $this->input->post('civilite') ,
					'NOM_PA' => $this->input->post('nom') ,
					'PRENOM_PA' => $this->input->post('prenom')
				);
                                $compte = array(
                                    'EMAIL'=>$email ,
                                    'MDP'=> $this->input->post('mdp'),
                                    'ETAT'=>0
                                );
				$this->mod_patient->addComptePatient($compte);
                                echo "OK";
                      }
	}


	
	function updateP1 (){
		$id_patient = $this->input->get('id');
		if (isset($_POST['submit']))
		{
			$jour = $this->input->post('jour');
			$mois = $this->input->post('mois');
			$annee = $this->input->post('annee');
			$date_naissance = $annee."-".$mois."-".$jour;
			$data = array(
				'id_patient'=> $id_patient, 
				'civilite' => $this->input->post('civilite'),
				'nom' => $this->input->post('nom'),
				'prenom' => $this->input->post('prenom'),
				'date_naissance' => $date_naissance,				
				'adresse' => $this->input->post('adresse'),
				'tel_mobile' => $this->input->post('tel_mobile'),
				'tel_fixe' => $this->input->post('tel_fixe'),
				'id_ville' => $this->input->post('ville'),
				'id_pays' => $this->input->post('pays'),
				'email' => $this->input->post('email'),
				'mdp' => $this->input->post('mdp'),
				'code_postal' => $this->input->post('code_postal')
			);
			$this->MDn->updateP1($data);
			$data['result']= $this->MDn->getInfoPatientId($id_patient);
			if($this->MDn->verifDD($id_patient)==1){
				$data['result1']= $this->MDn->getDemandeDevisId($id_patient);
			}			
			$data['pays']= $this->MDn->getAllPays();
			$data['ville']= $this->MDn->getAllVille(1);
			echo "<script>alert('Vos données personnelles ont bien été enregistrées')</script>";
			$this->load->vars($data);
			$this->load->view('pagedn/header',$data);
			$this->load->view('pagedn/updateP1affiche');
			$this->load->view('pagedn/footer');
		}
		else {
			$data['result']= $this->MDn->getInfoPatientId($id_patient);
			if($this->MDn->verifDD($id_patient)==1){
				$data['result1']= $this->MDn->getDemandeDevisId($id_patient);
			}else{
				$data2 = array(
					'id_patient'=> $id_patient
				);
				$this->MDn->insertDD($data2);
			}			
			$data['pays']= $this->MDn->getAllPays();
			$data['ville']= $this->MDn->getAllVille(1);
			$this->load->view('pagedn/header');
			$this->load->view('pagedn/updateP1', $data);
			$this->load->view('pagedn/footer');	
		}	
		
	}
	
	function listSelectionClinic(){
		$data['result'] ="";
		$id_patient = $this->input->get('id');
		$list= $this->MDn->getAlllistSelectionClinic($id_patient);
		if($list != ""){
			$input = explode(",", $list);
			$result = array_unique($input);
			$data['result'] = $result;
		}
		$this->load->view('pagedn/header');
		$this->load->view('pagedn/listSelectionClinic', $data);
		$this->load->view('pagedn/footer');	
	}
	
	function updateP1affiche (){
		$id_patient = $this->input->get('id');
		$data['result']= $this->MDn->getInfoPatientId($id_patient);
		$data['pays']= $this->MDn->getAllPays();
		$data['ville']= $this->MDn->getAllVille(1);
		$this->load->view('pagedn/header');
		$this->load->view('pagedn/updateP1affiche', $data);
		$this->load->view('pagedn/footer');
			
	}

	function insertDD(){
		$id_patient = $this->input->get('id');
		if (isset($_POST['submit']))
		{
			$this->load->library('upload');  
			for($i=1; $i<=5; $i++){
				$filename = 'photo'.$i;
				if (!empty($_FILES[$filename]['name']))
				{
				    // Specify configuration for File          
				    $config['upload_path'] = 'upload/';
				    $config['allowed_types'] = 'gif|jpg|png';
				    $config['max_size'] = '200000';
				    $config['max_width']  = '1024';
				    $config['max_height']  = '768';  
				    // Initialize config for File 
				    $this->upload->initialize($config);
				    // Upload file 
				    if ($this->upload->do_upload($filename))
				    {
				        $data = $this->upload->data();
				        $url = $config['upload_path']."".$_FILES[$filename]['name'];
				    }
				    else
				    {
				        echo $this->upload->display_errors();
				    }
				}
	        	}
			$data = array(
				'id_patient' => $id_patient,
				'photo1' => $_FILES["photo1"]['name'],
				'photo2' => $_FILES["photo2"]['name'],
				'photo3' => $_FILES["photo3"]['name'],
				'photo4' => $_FILES["photo4"]['name'],
				'photo5' => $_FILES["photo5"]['name'],
				'budget1' => $this->input->post('budget1'),
				'budget2' => $this->input->post('budget2'),
				'solution_souhaite' => $this->input->post('solution_souhaite'),
				'nb_traitement' => $this->input->post('nb_traitement'),
				'moment_soin' => $this->input->post('moment_soin'),
				'antecedent' => $this->input->post('antecedent'),
				'diabete' => $this->input->post('diabete'),
				'tension' => $this->input->post('tension'),
				'dialyse' => $this->input->post('dialyse'),
				'greffe' => $this->input->post('greffe'),
				'insuf_card' => $this->input->post('insuf_card'),
				'hemophilie' => $this->input->post('hemophilie'),
				'autre_antecedent' => $this->input->post('autre_antecedent'),
				'premiere_langue' => $this->input->post('premiere_langue'),
				'deuxieme_langue' => $this->input->post('deuxieme_langue'),
				'souhait' => $this->input->post('souhait'),
				'voyage' => $this->input->post('voyage')
			);
			if($this->MDn->verifDD($id_patient)==1){
				$this->MDn->updateDD($data);
			}
			else{
				$this->MDn->insertDD($data);
			}	
			$data['result']= $this->MDn->getInfoPatientId($id_patient);
			$data['result1']= $this->MDn->getDemandeDevisId($id_patient);
			$data['pays']= $this->MDn->getAllPays();
			$data['ville']= $this->MDn->getAllVille(1);
			$data['tabs']='1';
			echo "<script>alert('Votre demande de devis a bien été enregistrée')</script>";
			$this->load->vars($data);
			$this->load->view('pagedn/header');
			$this->load->view('pagedn/updateP1');
			$this->load->view('pagedn/footer');
		}
		else {
			$data['result']= $this->MDn->getInfoPatientId($id_patient);
			$data['result1']= $this->MDn->getDemandeDevisId($id_patient);
			$data['pays']= $this->MDn->getAllPays();
			$data['ville']= $this->MDn->getAllVille(1);
			$this->load->view('pagedn/header');
			$this->load->view('pagedn/updateP1', $data);
			$this->load->view('pagedn/footer');	
		}	
	}

	
	function verifemail() {
		$email = $this->input->post('email');
		$result = $this->MDn->verifEmailPatient($email);
		if ($result==1){
			echo "tsok";
		}
		else {
			$result = $this->MDn->verifEmailClinic($email);
			if ($result==1){
				echo "tsok";
			}
			else {
				echo "ok";
			}
		}
	}

	function modifEmailPatient() {
		$email = $this->input->post('email');
		$id_patient = $this->input->post('id_patient');
                //$email = 'cedrick.binesse@yahoo.fr';
                //$id_patient = 'repertoire16';        
		if($email !="" && $id_patient !=""){
			$result = $this->MDn->updateEmailPatient($email, $id_patient);
			echo 1;
		}
		else {
			echo 0;
		}
	}

	function modifQuiSuisJe() {
		$civilite = $this->input->post('civilite');
		$nom = $this->input->post('nom');
		$prenom = $this->input->post('prenom');
		$date_naissance = $this->input->post('date_naissance');
		$id_patient = $this->input->post('id_patient');
		if($civilite !="" && $nom !="" && $prenom !=""){
			$result = $this->MDn->updateQuiSuisJePatient($civilite, $nom, $prenom, $date_naissance, $id_patient);
			echo 1;
		}
		else {
			echo 0;
		}
	}
	
	function modifDemandeDevisSoinSouhaite() {
		$budget1 = $this->input->post('budget1');
		$budget2 = $this->input->post('budget2');
		$solution_souhaite = $this->input->post('solution_souhaite');
		$nb_traitement = $this->input->post('nb_traitement');
		$moment_soin = $this->input->post('moment_soin');
		$id_patient = $this->input->post('id_patient');
		if($budget1 !="" && $budget2 !="" && $solution_souhaite !="" && $nb_traitement !="" && $moment_soin !=""){
			if($this->MDn->verifDD($id_patient)==1){
				$result = $this->MDn->updateDemandeDevisSoinSouhaite($budget1, $budget2, $solution_souhaite, $nb_traitement, $moment_soin, $id_patient);
			}
			else{
				$data = array(
				'budget1' => $budget1 ,
				'budget2' => $budget2 ,
				'solution_souhaite' => $solution_souhaite ,
				'nb_traitement' => $nb_traitement ,
				'moment_soin' => $moment_soin ,
				'id_patient' => $id_patient 
				);
				$this->MDn->insertDD($data);
			}
			echo 1;
		}else{
			echo 0;
		}
	}
	
	function modifDemandeDevisAntMed() {
		$diabete = $this->input->post('diabete');
		$tension = $this->input->post('tension');
		$dialyse = $this->input->post('dialyse');
		$greffe = $this->input->post('greffe');
		$insuf_card = $this->input->post('insuf_card');
		$hemophilie = $this->input->post('hemophilie');
		$autre_antecedent = $this->input->post('autre_antecedent');
		$antecedent = $this->input->post('antecedent');
		$id_patient = $this->input->post('id_patient');
		if($id_patient !=""){
			if($this->MDn->verifDD($id_patient)==1){
				$result = $this->MDn->updateDemandeDevisAntMed($diabete, $tension, $dialyse, $greffe, $insuf_card, $hemophilie, $autre_antecedent, $antecedent, $id_patient);
			}
			else{
				$data = array(
				'diabete' => $diabete ,
				'tension' => $tension ,
				'dialyse' => $dialyse ,
				'greffe' => $greffe ,
				'insuf_card' => $insuf_card ,
				'hemophilie' => $hemophilie ,
				'autre_antecedent' => $autre_antecedent ,
				'antecedent' => $antecedent ,
				'id_patient' => $id_patient 
				);
				$this->MDn->insertDD($data);
			}
			echo 1;
		}else{
			echo 0;
		}
	}

	function devisRecusPatient (){
		$id_patient = $this->input->get('id');
		$data['result']= $this->MDn->getAllDevisRecusPatient($id_patient);
		$this->load->view('pagedn/devisRecusPatient', $data);
	}
	
	function modifDemandeDevisCom() {
		$premiere_langue = $this->input->post('premiere_langue');
		$deuxieme_langue = $this->input->post('deuxieme_langue');
		$voyage = $this->input->post('voyage');
		$souhait = $this->input->post('souhait');
		$id_patient = $this->input->post('id_patient');
		//echo $voyage.":".$id_patient;
		if($id_patient !=""){
			if($this->MDn->verifDD($id_patient)==1){
				$result = $this->MDn->updateDemandeDevisCom($premiere_langue, $deuxieme_langue, $voyage, $souhait, $id_patient);
				echo 1;
			}
			else{
				$data = array(
				'premiere_langue' => $premiere_langue ,
				'deuxieme_langue' => $deuxieme_langue ,
				'voyage' => $voyage ,
				'souhait' => $souhait ,
				'id_patient' => $id_patient 
				);
				$this->MDn->insertDD($data);
				echo 1;
			}
		}else{
			echo 0;
		}
	}
	
	function modifDemandeDevisImages() {
		$photo1 = $this->input->post('photo1');
		$photo2 = $this->input->post('photo2');
		$photo3 = $this->input->post('photo3');
		$photo4 = $this->input->post('photo4');
		$photo5 = $this->input->post('photo5');
		$id_patient = $this->input->post('id_patient');
		if($id_patient !=""){
			if($this->MDn->verifDD($id_patient)==1){
				$result = $this->MDn->updateDemandeDevisImages($photo1, $photo2, $photo3, $photo4, $photo5, $id_patient);
			}
			else{
				$data = array(
				'photo1' => $photo1 ,
				'photo2' => $photo2 ,
				'photo3' => $photo3 ,
				'photo4' => $photo4 ,
				'photo5' => $photo5 ,
				'id_patient' => $id_patient 
				);
				$this->MDn->insertDD($data);
			}
			echo 1;
		}else{
			echo 0;
		}
	}
	
	function modifLocalitePatient() {
		$adresse = $this->input->post('adresse');
		$id_pays = $this->input->post('pays');
		$id_ville = $this->input->post('ville');
		$code_postal = $this->input->post('code_postal');
		$id_patient = $this->input->post('id_patient');
		if($adresse !="" && $code_postal !=""){
			$result = $this->MDn->updateLocalitePatient($adresse, $code_postal, $id_pays, $id_ville, $id_patient);
			echo 1;
		}
		else {
			echo 0;
		}
	}
	
	function modifMejoindrePatient() {
		$email = $this->input->post('email');
		$tel_fixe = $this->input->post('tel_fixe');
		$tel_mobile = $this->input->post('tel_mobile');
		$id_patient = $this->input->post('id_patient');
                $isPartageContact1 = $this->input->post('isPartageContact1');
                $isPartageContact2 = $this->input->post('isPartageContact2');
		if($email !="" && $tel_fixe !=""&& $tel_mobile !=""){
			$result = $this->MDn->updateMeJoindrePatient($email, $tel_fixe, $tel_mobile, $id_patient,$isPartageContact1,$isPartageContact2);
			echo 1;
		}
		else {
			echo 0;
		}
	}
	
	function verifComptePatient() {
		$email = $this->input->post('email');
		$mdp = $this->input->post('mdp');
		$result = $this->MDn->verifComptePatient($email,$mdp);
		if ($result!=0){
			echo $result;
			//$data = $this->MClient->getInfoPatient($email);
			//foreach ($data as $key => $list){	  					
			$newdata = array(
			   'idemail'  => $email,
			   'logged_in' => 'patient',
			    'idid' => $result
		    );          
		    $this->session->set_userdata($newdata);
		    $_SESSION['idemail'] = $email;	
		    $_SESSION['logged_in'] = 'patient';	
		     $_SESSION['idid'] = $result;	
			//}	
		}
		else {
			echo "ts ok";
		}
	}
	
	function verifComptePatientEnter() {
                $this->form_validation->set_rules('email','"Adresse mail"','trim|required|xss_clean|valid_email');
                $this->form_validation->set_rules('password_patient','"Mot de passe"','trim|required|xss_clean');
                if($this->form_validation->run()==FALSE){
                    $this->load->view('accueil');
                } else {
		$email = $this->input->post('email');
		$mdp = $this->input->post('password_patient');
		$result = $this->MDn->verifComptePatient($email,$mdp);
		if ($result!=0){
			//echo $result;
			//$data = $this->MClient->getInfoPatient($email);
			//foreach ($data as $key => $list){	  					
			$newdata = array(
			 'idemail'  => $email,
			 'logged_in' => 'patient',
			'idid' => $result
		   );          
                    $this->session->set_userdata($newdata);
                    $_SESSION['idemail'] = $email;	
                    $_SESSION['logged_in'] = 'patient';	
                    $_SESSION['idid'] = $result;
			//header("location:updateP1?id=".$result);
                   //echo '<script>document.location = "updateP1?id='.$result.'";</script>';
                   echo '<script>document.location="espace_patient?id='.$result.'";</script>';
                    //création attribut id_patient
		}
		else {
			//echo "ts ok";
			/*echo '<script>alert("Erreur login ou mot de passe")</script>';
			$this->load->view('pagedn/header');	
			$this->load->view('pagedn/ajoutP1');	
			$this->load->view('pagedn/footer');	*/
			echo '<script>alert("Erreur login ou mot de passe"); document.location = "../index.php";</script>';
                    
		}
            }
        }
	
	function mdpoublie() {
		$email = $this->input->post('email');
		$result = $this->MDn->getInfoPatient($email);
		foreach ($result as $key => $list){
			$mdp = $list['mdp'];
			$this->envoiMailMdpOUbilie($email, $mdp);
		}
	}	

	
	function init (){
		$email = $this->input->get('id');
		$email = $this->base64url_decode($email);
		$this->MDn->updateIsValidPatient($email);
		$this->load->view('pagedn/header');	
		$this->load->view('pagedn/ajoutP1');	
		$this->load->view('pagedn/footer');		
		
	}
	
	private function base64url_decode($plainText) {
	    $base64url = strtr($plainText, '-_,', '+/=');
	    $base64 = base64_decode($base64url);
	    return $base64;  
	}
	
	function devisTypePatient(){
		$id_patient = $this->input->get('id');
		$id_clinique = $this->input->get('idc');
		$data['result']= $this->MDn->getInfoPatientId($id_patient);
		$data['resultDevis1']= $this->MDn->getDevisTableau($id_patient, $id_clinique,1);
		$data['resultDevis2']= $this->MDn->getDevisTableau($id_patient, $id_clinique,2);
		$data['resultDevis3']= $this->MDn->getDevisTableau($id_patient, $id_clinique,3);
		$data['resultDevis4']= $this->MDn->getDevisTableau($id_patient, $id_clinique,4);
		if($this->MDn->verifDD($id_patient)==1){
			$data['result1']= $this->MDn->getDemandeDevisId($id_patient);
		}
		$data['id_patient']= $id_patient;
		$data['id_clinique']= $id_clinique;
		$data['result2']= $this->MDn->getConditionEtGarantie($id_clinique);
		$data['result3']= $this->MDn->getInfoClinicId($id_clinique);
		$data1 = array();
		$photo1 = $this->MDn->selectImagedemandeDevis($id_patient, 1);
		$photo2 = $this->MDn->selectImagedemandeDevis($id_patient, 2);
		$photo3 = $this->MDn->selectImagedemandeDevis($id_patient, 3);
		$photo4 = $this->MDn->selectImagedemandeDevis($id_patient, 4);
		$photo5 = $this->MDn->selectImagedemandeDevis($id_patient, 5);
		if ($photo1!=""){
			$data1['photo1']= $photo1;
		}
		if ($photo2!=""){
			$data1['photo2']= $photo2;
		}
		if ($photo3!="" ){
			$data1['photo3']= $photo3;
		}
		if ($photo4!="" ){
			$data1['photo4']= $photo4;
		}
		if ($photo5!="" ){
			$data1['photo5']= $photo5;
		}
		
		if ( $photo1!="" || $photo2!="" || $photo3!="" || $photo4!="" || $photo5!=""){
			$data ['resultP'] = $data1;
		}
		else {
			$data ['resultP'] = 0;
		}
		$data2 = array();
		$langue1 = $this->MDn->selectLanguedemandeDevis($id_patient, 1);
		$langue2 = $this->MDn->selectLanguedemandeDevis($id_patient, 2);
		$langue3 = $this->MDn->selectLanguedemandeDevis($id_patient, 3);
		if ($langue1!="Selectionner"){
			$data2['langue1']= $langue1;
		}
		if ($langue2!="Selectionner"){
			$data2['langue2']= $langue2;
		}
		if ($langue3!="Selectionner" ){
			$data2['langue3']= $langue3;
		}
		if ( $langue1!="Selectionner" || $langue3!="Selectionner" || $langue2!="Selectionner"){
			$data ['resultL'] = $data2;
		}
		else {
			$data ['resultL'] = 0;
		}
		$data['pays']= $this->MDn->getAllPays();
		$this->load->vars($data);
		$this->load->view('pagedn/devisTypePatient');
	}
	
	private function envoiMailPatient($email, $id_patient) {
		$this->load->helper('email');
		$this->load->library('email');
		$this->email->set_mailtype("html");
		//$this->load->library('email');
		//header('Content-type: <type>; charset=utf-8');
		$string = $email;
		$string = chunk_split(base64_encode($string), 64, "\n");
		$url = base_url()."index.php/patient/init?id=$string";
		$message = "Félicitations! Voous venez de créer votre espace patient. <br /> ";
		//$message = $message."Cliquez <a href='$url"."index.php/dn/updateP1?id=$id_patient'>ici</a> pour pour accéder à votre espace personnel.";
		$message = $message."Cliquez <a href='$url'>ici</a> pour remplir une demande de devis afin de pouvoir l'envoyer à des cliniques.";
		$this->email->from('centredentaire@centredentaire.net', 'Centre dentaire');
		$this->email->to($email);
		$this->email->cc('aimejules1@gmail.com');  
		//$this->email->cc('rmijanirina@yahoo.fr'); 
		//$this->email->bcc('them@their-example.com');
		$this->email->subject('Confirmation inscription');
		$this->email->message($message);
		$this->email->send();
		//echo $this->email->print_debugger();
		//echo "email envoye" ;
	}
	
	private function envoiMailMdpOUbilie($email, $mdp) {
		$this->load->helper('email');
		$this->load->library('email');
		$this->email->set_mailtype("html");
		header('Content-type: <type>; charset=utf-8');
		$url = base_url();
		$message = "Votre mot de passe est $mdp<br /> ";
		$message = $message."";
		$this->email->from('centredentaire@centredentaire.net', 'Centre dentaire');
		$this->email->to($email);
		$this->email->cc('domoinaalivelo@gmail.com');  
		$this->email->subject('Recuperation mot de passe');
		$this->email->message($message);
		$this->email->send();
		//echo "envoie email ".$email."<br />";
	}	
	
	function communication(){
		$data1 = array();
		$id_patient = $this->input->get('id');
		$langue1 = $this->MDn->selectLanguedemandeDevis($id_patient, 1);
		$langue2 = $this->MDn->selectLanguedemandeDevis($id_patient, 2);
		$langue3 = $this->MDn->selectLanguedemandeDevis($id_patient, 3);
		if ($langue1!="Selectionner"){
			$data1['langue1']= $langue1;
		}
		if ($langue2!="Selectionner"){
			$data1['langue2']= $langue2;
		}
		if ($langue3!="Selectionner" ){
			$data1['langue3']= $langue3;
		}
		if ( $langue1!="Selectionner" || $langue3!="Selectionner" || $langue2!="Selectionner"){
			$data ['result'] = $data1;
		}
		else {
			$data ['result'] = 0;
		}
		$this->load->vars($data);
		$this->load->view('pagedn/communication');
	}
	
	function communicationSave(){
		$id_patient = $this->input->post('id_patient');
		$butonLangage = $this->input->post('butonLangage');
		$Langage = $this->input->post('langage');
		$id_Langage = $this->input->post('id_Langage');
		//butonLangage : butonLangage, langage : Langage, id_patient : id_clinic, id_Langage : id_Langage
		//echo $butonLangage.":".$Langage.":".$id_Langage.":".$id_patient;
		if ($butonLangage == "Oui"){
			$this->MDn->updateLanguedemandeDevis($butonLangage, $id_patient, $Langage, $id_Langage, "Oui");
		}
		else if ($butonLangage == "Non"){
			$this->MDn->updateLanguedemandeDevis($butonLangage, $id_patient, $Langage, $id_Langage, "Non");
		}
		
	}
	
	function photoDemandeDevis(){
		$data1 = array();
		$id_patient = $this->input->get('id');
		$photo1 = $this->MDn->selectImagedemandeDevis($id_patient, 1);
		$photo2 = $this->MDn->selectImagedemandeDevis($id_patient, 2);
		$photo3 = $this->MDn->selectImagedemandeDevis($id_patient, 3);
		$photo4 = $this->MDn->selectImagedemandeDevis($id_patient, 4);
		$photo5 = $this->MDn->selectImagedemandeDevis($id_patient, 5);
		if ($photo1!=""){
			$data1['photo1']= $photo1;
		}
		if ($photo2!=""){
			$data1['photo2']= $photo2;
		}
		if ($photo3!="" ){
			$data1['photo3']= $photo3;
		}
		if ($photo4!="" ){
			$data1['photo4']= $photo4;
		}
		if ($photo5!="" ){
			$data1['photo5']= $photo5;
		}
		
		if ( $photo1!="" || $photo2!="" || $photo3!="" || $photo4!="" || $photo5!=""){
			$data ['result'] = $data1;
		}
		else {
			$data ['result'] = 0;
		}
		$this->load->vars($data);
		$this->load->view('pagedn/photoDemandeDevis');
	}
	
	function afficheImage(){
		$this->load->view('pagedn/afficheImage');
	}
	
	function msgRecus (){
		$id_patient = $this->input->get('id');
		$data['id_patient']= $id_patient;
		$data['result']= $this->MDn->getAllMsgPatientRecus($id_patient,1);
		$this->load->view('pagedn/msgPatientsRecus', $data);
	}
	
	function supprMessagePatient (){
		$id_message = intval($this->input->get('id'));
		$id_patient = intval($this->input->get('id_patient'));
		$this->MDn->deleteMessageClinic($id_message);
		echo '<script>alert("Supprimé");</script>';
		$data['result']= $this->MDn->getAllMsgPatientRecus($id_patient,1);
		$this->load->vars($data);
		$this->load->view('pagedn/msgPatientsRecus');
	}
	
	function fichePatient(){
		$id_patient = $this->input->get('id');
		$data['result']= $this->MDn->getInfoPatientId($id_patient);
		if($this->MDn->verifDD($id_patient)==1){
			$data['result1']= $this->MDn->getDemandeDevisId($id_patient);
		}
		$data1 = array();
		$photo1 = $this->MDn->selectImagedemandeDevis($id_patient, 1);
		$photo2 = $this->MDn->selectImagedemandeDevis($id_patient, 2);
		$photo3 = $this->MDn->selectImagedemandeDevis($id_patient, 3);
		$photo4 = $this->MDn->selectImagedemandeDevis($id_patient, 4);
		$photo5 = $this->MDn->selectImagedemandeDevis($id_patient, 5);
		if ($photo1!=""){
			$data1['photo1']= $photo1;
		}
		if ($photo2!=""){
			$data1['photo2']= $photo2;
		}
		if ($photo3!="" ){
			$data1['photo3']= $photo3;
		}
		if ($photo4!="" ){
			$data1['photo4']= $photo4;
		}
		if ($photo5!="" ){
			$data1['photo5']= $photo5;
		}
		
		if ( $photo1!="" || $photo2!="" || $photo3!="" || $photo4!="" || $photo5!=""){
			$data ['resultP'] = $data1;
		}
		else {
			$data ['resultP'] = 0;
		}
		$data2 = array();
		$langue1 = $this->MDn->selectLanguedemandeDevis($id_patient, 1);
		$langue2 = $this->MDn->selectLanguedemandeDevis($id_patient, 2);
		$langue3 = $this->MDn->selectLanguedemandeDevis($id_patient, 3);
		if ($langue1!="Selectionner"){
			$data2['langue1']= $langue1;
		}
		if ($langue2!="Selectionner"){
			$data2['langue2']= $langue2;
		}
		if ($langue3!="Selectionner" ){
			$data2['langue3']= $langue3;
		}
		if ( $langue1!="Selectionner" || $langue3!="Selectionner" || $langue2!="Selectionner"){
			$data ['resultL'] = $data2;
		}
		else {
			$data ['resultL'] = 0;
		}
		$data['pays']= $this->MDn->getAllPays();
		$data['ville']= $this->MDn->getAllVille(1);
		$this->load->vars($data);
		$this->load->view('pagedn/fichePatient');
	}
	
	function afficheMsgPatient(){
		$id_message = $this->input->get('id');
		$data['result']= $this->MDn->getMessageByIdMsg($id_message);
		$this->load->vars($data);
		$this->load->view('pagedn/afficheMsgPatient');
	}
	
	function envoiReponsePatient(){
		$id_patient = $this->input->post('id_patient');
		$id_clinique = $this->input->post('id_clinique');
		$txtobjet = $this->input->post('txtobjet');
		$reponsecontent = $this->input->post('reponsecontent');
		$isMsgPatient = 1;
		$date = date('Y-m-d');
		$email = $this->MDn->recupEmailClinic($id_clinique);
		$this->envoiReponsePatientMsg($email,$id_clinique);
		$data = array(
				'id_clinique' => $id_clinique,
				'id_patient'=>$id_patient,
				'objet'=>$txtobjet,
				'contenu'=>$reponsecontent,
				'date_envoi'=>$date,
				'isMsgPatient'=>$isMsgPatient
			);
		$this->MDn->insertMsgClinic($data);
	}
	
	private function envoiReponsePatientMsg($email,$id_clinique) {
		$this->load->helper('email');
		$this->load->library('email');
		$this->email->set_mailtype("html");
		header('Content-type: <type>; charset=utf-8');
		$url = base_url();
		$urlclinique = base_url()."index.php/clinic/detail?id=".$id_clinique;
		$message = "Bonjour, <br />vous avez reçu une réponse de la clinique <a href='$urlclinique'>espace clinique</a> .<br />Merci";
		$message = $message."Suivez ce <a href='$url'>lien</a> pour l\'examiner";
		$this->email->from('centredentaire@centredentaire.net', 'Centre dentaire');
		$this->email->to($email);
		$this->email->cc('domoinaalivelo@gmail.com');  
		$this->email->subject('Réponse d\'un message');
		$this->email->message($message);
		$this->email->send();
		//echo "envoie email ".$email."<br />";
	}
	
	
	function photoDemandeDevisSave(){
		$id_patient = $this->input->post('id_patient');
		$butonPhotoDemande = $this->input->post('butonPhotoDemande');
		$PhotoDemande = $this->input->post('PhotoDemande');
		$id_PhotoDemande = $this->input->post('id_PhotoDemande');
		//echo $butonPhotoDemande.":".$PhotoDemande.":".$id_PhotoDemande.":".$id_patient;
		if ($butonPhotoDemande == "Oui"){
			$this->MDn->updateImagedemandeDevis($butonPhotoDemande, $id_patient, $PhotoDemande, $id_PhotoDemande, "Oui");
		}
		else if ($butonPhotoDemande == "Non"){
			$this->MDn->updateImagedemandeDevis($butonPhotoDemande, $id_patient, $PhotoDemande, $id_PhotoDemande, "Non");
		}
	}
        function espace_patient (){
            $id_patient = $this->input->get('id');
            $data=array();
            $data['info_patient']= $this->MDn->getInfoPatientId($id_patient);
           $this->load->view('patient/vue_espace_patient',$data); 
        }
}
?>